package com.zhijian.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.TopicMapper;
import com.zhijian.medical.entity.po.Topic;
import com.zhijian.medical.entity.po.TopicHistory;
import com.zhijian.medical.entity.vo.request.TopicSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.TopicSearchVo;
import com.zhijian.medical.entity.vo.request.TopicUpdateVo;
import com.zhijian.medical.entity.vo.response.TopicVo;
import com.zhijian.medical.service.TopicHistoryService;
import com.zhijian.medical.service.TopicService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 人员课题业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-19 15:00:20
 */
@Slf4j
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    private TopicMapper mapper;

    @Autowired
    private TopicHistoryService topicHistoryService;

    @Override
    public Topic selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public TopicVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(Topic topic) {
        Date now = new Date();
        topic.setCreateTime(now);
        topic.setUpdateTime(now);
        return mapper.insert(topic) > 0 ? topic.getId() : 0L;
    }

    @Override
    public boolean update(Topic topic) {
        Date now = new Date();
        topic.setUpdateTime(now);
        return mapper.updateByPrimaryKey(topic) != 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean update(TopicUpdateVo topicUpdateVo) {
        Topic oldTopic = this.selectById(topicUpdateVo.getId());
        AssertUtil.notNull(oldTopic, "此课题不存在或者已被删除");
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicUpdateVo, topic);
        this.update(topic);
        TopicHistory topicHistory = new TopicHistory();
        BeanUtils.copyProperties(topicUpdateVo, topicHistory);
        topicHistory.setTopicId(topicUpdateVo.getId());
        return topicHistoryService.insert(topicHistory) != 0;
    }

    @Override
    public Integer count(TopicSearchVo topicSearchVo) {
        return mapper.count(topicSearchVo);
    }

    @Override
    public List<TopicVo> selectList(TopicSearchVo topicSearchVo) {
        return mapper.selectList(topicSearchVo);
    }

    @Override
    public Page<TopicVo> selectPage(TopicSearchVo topicSearchVo) {
        //先直接查总数
        int count = count(topicSearchVo);
        //分页
        Page<TopicVo> page = new Page<>(true, count, topicSearchVo.getPageNum(), topicSearchVo.getPageSize());
        topicSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TopicVo> responseVos = this.selectList(topicSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean save(TopicSaveOrUpdateVo topicSaveOrUpdateVo) {
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicSaveOrUpdateVo, topic);
        if (StringUtils.isNotBlank(topicSaveOrUpdateVo.getAcquireTime())) {
            topic.setAcquireTime(DateUtil.parseDate(topicSaveOrUpdateVo.getAcquireTime()));
        }
        return this.insert(topic) != 0;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean modify(TopicSaveOrUpdateVo topicSaveOrUpdateVo) {
        Topic oldTopic = this.selectById(topicSaveOrUpdateVo.getId());
        AssertUtil.notNull(oldTopic, "此课题不存在或者已被删除");
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicSaveOrUpdateVo, topic);
        if (StringUtils.isNotBlank(topicSaveOrUpdateVo.getAcquireTime())) {
            topic.setAcquireTime(DateUtil.parseDate(topicSaveOrUpdateVo.getAcquireTime()));
        }
        this.update(topic);
        TopicHistory topicHistory = new TopicHistory();
        BeanUtils.copyProperties(topicSaveOrUpdateVo, topicHistory);
        topicHistory.setTopicId(topicSaveOrUpdateVo.getId());
        return topicHistoryService.insert(topicHistory) != 0;
    }

}